//index.js
//获取应用实例
import {login,getCode,codeLogin} from "../../ajax/module/userInfo.js"
import imgUrl from "../../images/util.js"

var app = getApp();

Page({
  data: {
  
    userInfo: {},
    phone:"",
    pwd:"",
    logo:imgUrl.logoBase64,
    flg:false,
    code:"获取验证码",
    isVilidata:true
    
   
  },
  inputPhone(e){
    if(e.detail.value.length == 11){
       this.setData({
         isVilidata:false
       })
    }else{
      this.setData({
        isVilidata:true
      })
    }
     this.setData({
       phone:e.detail.value
     })
  },
  inputPwd(e){
    this.setData({
       pwd:e.detail.value
    })
  },
  getCode(){
    console.log("嘿嘿嘿")
    
    this.timeStamp()
  },
  toLogin(){
    // loginName  smsCode
    let data={
      loginName:this.data.phone+"",
      smsCode:this.data.pwd+""
    }
    codeLogin(data).then(res=>{
      this.setData({
        userInfo:res.data
      })
      let token=res.data.token
        let enterName=res.data.enterName
        let address=res.data.address
        app.globalData.loginInfoStore={enterName,address}
        app.globalData.account={
          loginName:this.data.phone+"",
          loginPwd:this.data.pwd+""
        }
        app.globalData.associatedLicense={
          licenseCode:res.data.licenseCode,
          licenseImgUrl:res.data.licenseImgUrl
        }

        wx.showToast({
          icon:"none",
          title:"登录成功",
          duration:2000
        })
        let {status}=res.data

        setTimeout(()=>{
          wx.setStorage({
            key: 'token',
            data: token,
           
          })
          wx.setStorage({
            data: this.data.phone,
            key: 'loginName',
          })
          wx.setStorage({
            data: true,
            key: 'isLogin',
          })
          wx.setStorage({
            data: status,
            key: 'status',
          })

  
          wx.setStorage({
            data: {enterName,address},
            key: 'loginInfoStore',
          })
          if(status == 1){
         
    
            wx.setStorage({
              data: {
                licenseCode:res.data.licenseCode,
                licenseImgUrl:res.data.licenseImgUrl,
                enterName:res.data.enterName
              },
              key: 'associatedLicense',
            })
           
            wx.reLaunch({
              url: '../store-manager/store-manager'         
            })
          }else{
            // 跳转到 商家入驻
            wx.setStorage({
              data: loginName,
              key: 'loginName',
            })
            wx.setStorage({
              data: loginPwd,
              key: 'abcKey',
            })
            wx.navigateTo({
              url: '../businessInit/businessInit'         
            })
          }
        
        },1000)


    }).catch(err=>{
      // if(this.data.phone ==)
      wx.setStorage({
        data: this.data.phone+"",
        key: 'loginName',
      })
      // wx.setStorage({
      //   data: loginPwd,
      //   key: 'abcKey',
      // })
      wx.navigateTo({
        url: '../businessInit/businessInit'         
      })
    })
  },
  //事件处理函数
  login() {
   console.log("555")
    let loginName=this.data.phone+""
    let loginPwd=this.data.pwd+""
    

    login({loginName,loginPwd}).then(res=>{
        this.setData({
          userInfo:res.data
        })
//         accountCertificateUrl: null
// address: "广东省广州市白云区鹤龙一路"
// bizUserId: 28
// bossIdCard: "440882198509271458"
// bossName: "蔡先生"
// contactEmail: null
// contactPhone: "13760773908"
// contactUser: "梅姐"
// customerTel: "13760773908"
// enterName: "蔡氏家政"
// enterpriseType: 4
// licenseCode: "45966879HU699"
// licenseImgUrl: "http://tiantue-image.oss-cn-shenzhen.aliyuncs.com/images/202101/141610609228617.jpg"
// loginName: "13760773908"
// status: 1
// token: "ea294bc042704807ba724d1c42991ef8"
// userId: 16690

        let token=res.data.token
        let enterName=res.data.enterName
        let address=res.data.address
        app.globalData.loginInfoStore={enterName,address}
        app.globalData.account={
          loginName:loginName,
          loginPwd:loginPwd
        }
        app.globalData.associatedLicense={
          licenseCode:res.data.licenseCode,
          licenseImgUrl:res.data.licenseImgUrl
        }

        wx.showToast({
          icon:"none",
          title:"登录成功",
          duration:2000
        })
        // let {status}=res.data

        let status =2

        setTimeout(()=>{
          wx.setStorage({
            key: 'token',
            data: token,
           
          })
          wx.setStorage({
            data: true,
            key: 'isLogin',
          })
          wx.setStorage({
            data: status,
            key: 'status',
          })

  
          wx.setStorage({
            data: {enterName,address},
            key: 'loginInfoStore',
          })
          if(status == 1){
         
    
            wx.setStorage({
              data: {
                licenseCode:res.data.licenseCode,
                licenseImgUrl:res.data.licenseImgUrl,
                enterName:res.data.enterName
              },
              key: 'associatedLicense',
            })
           
            wx.reLaunch({
              url: '../store-manager/store-manager'         
            })
          }else{
            // 跳转到 商家入驻
            wx.setStorage({
              data: loginName,
              key: 'loginName',
            })
            wx.setStorage({
              data: loginPwd,
              key: 'abcKey',
            })
            wx.reLaunch({
              url: '../businessInit/businessInit'         
            })
          }
        
        },1000)
      
       
    })
  },
  onLoad() {
    // getApp().globalData.name = "王二麻子"
      
   
  },
  timeStamp(){
    getCode(this.data.phone).then(res=>{
      // console.log(res.data)
      wx.showToast({
        title: '验证码获取成功',
        duration:2000
      })
      let time =60
    
    this.setData({
      code:time+("s"),
      flg:true
    })
    let temp=setInterval(() => {
      time = time-1
      // console.log(time)
      this.setData({
        code:time+("s")
      })
      if(time == 0){
        this.setData({
          code:"重新发送",
          flg:false
        })
        clearInterval(temp)
      }
    }, 1000);
    })

    
  },
  onShow(){
    new Promise((resolve,reject)=>{
        wx.getStorage({
          key: 'status',
          success:res=>{
             resolve(res.data)
          }
        })

    }).then(param=>{
        if(param == 1){
          wx.getStorage({
            key: 'loginInfoStore',
            success:res=>{
              app.globalData.loginInfoStore=res.data
              wx.reLaunch({
                url: '../store-manager/store-manager',
              })
            }
          })
         
        }
        // else{
        //   wx.navigateTo({
        //     url: '../businessInit/businessInit',
        //   })
        // }
    })
   
  }
  
})
